
#include "TUIRead.h"
static int TUI_ReadStep = 0;
int TUI_DT_Parse(char* buffer, int len)
{
  int i=0;
  char *p;
  if(buffer[4] == 'M' && buffer[5] == 'T') // get the temperature
  {//e.g "ssDTMT 224,219,223,215,13240,395.85,395.89,395.85,395.90,302.46"
    i=0;
    p = strtok(buffer, ",");
    while(p) 
    {
      i++;
      if(i==10)
        m_TUIStatus.temperature = (short)(atof(p)-272.65);
      //printf("%s\n", p);
      p = strtok(NULL, ",");
    }
    //printf("temperature:%d\n", m_TUIStatus.temperature );
    return 1;
  }
  else if(buffer[4] == 'P' && buffer[5] == 'R') // get the pressure
  {//ssDTPR 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3040,0,0

    i=0;
    p = strtok(buffer, ",");
    while(p) 
    {
      i++;
      switch(i)
      {
      case 11:
      case 12:
      case 13:
        m_TUIStatus.CHs[i-11].nowPres.value_set = (long)atoi(p);
        break;
      case 24:
      case 25:
      case 26:
        m_TUIStatus.CHs[i-24].nowPres.value_act = (long)atoi(p);
        m_TUIStatus.CHs[i-24].Value[0].value_act = (long)atoi(p);
        break;
      }
      //printf("%s\n", p);
      p = strtok(NULL, ",");
    }
    return 1;

    /*
    ssDTPR <FIPs>,<FIFs>,<BIPs>,<BIFs>,<FD1Ps>,<FD2Ps>,<FD3Ps>,
    <BD1Ps>,<BD2Ps>,<BD3Ps>, <AX3Ps>,<AX4Ps>,<AX5Ps> 
    <FIPa>,<FIFa>,<BIPa>,<BIFa>,<FD1Pa>,<FD2Pa>,<FD3Pa>,
    <BD1Pa>,<BD2Pa>,<BD3Pa>, <AX3Pa>,<AX4Pa>,<AX5Pa> 
    */

  }
}

int TUI_AUX_Parse(char* buffer, int len)
{
  int i=0,CHItem = (buffer[3]-'3');
  char *p;
  if(buffer[4] == 'N' && buffer[5] == 'Z') // get the on/off
  {
    //e.g "ssA3NZ 1"
    m_TUIStatus.CHs[CHItem].isON = buffer[7] - '0';
    return 1;
  }
  else if(buffer[4] == 'S' && buffer[5] == 'T') // the current pneumatics status
  {
    //e.g "ssA3ST 1,1000,1200"
    m_TUIStatus.CHs[CHItem].isON = buffer[7]-'0';

    i=0;
    p = strtok(buffer, ",");
    while(p) 
    {
      i++;
      switch(i)
      {
      case 2:
        m_TUIStatus.CHs[CHItem].nowPres.value_set = (long)atoi(p);
        //m_TUIStatus.CHs[CHItem].Value[0].value_set = (long)atoi(p);
        break;
      case 3:
        m_TUIStatus.CHs[CHItem].nowPres.value_act = (long)atoi(p);
        m_TUIStatus.CHs[CHItem].Value[0].value_act = (long)atoi(p);
        break;
      }
      //printf("%s\n", p);
      p = strtok(NULL, ",");
    }
    return 1;

  }
  return 0;
}
int TUI_GC_Parse(char* buffer, int len)
{
  if(buffer[4] == 'P' && buffer[5] == 'R') // get the on/off
  {
    if(buffer[7] == '0')
    {
      m_TUIStatus.runState = PRE_RUN;
      Run_State = RUN_ACTIVE;//begin run
    }
    else
      m_TUIStatus.runState = RUN_IDLE;
    return 1;
  }
  return 0;
}
int TUI_packetParse(char* buffer, int len)
{
  char buffCpy[200];
  if(len < 6)
  {
    mLog("packet buffer length error!!\n");
    return 0;
  }
  else if(len > 190)
  {
    mLog("packet buffer length too largh!!!\n");
    return 0;
  }
  strcpy(buffCpy,buffer);
  if(buffCpy[2] == 'D' && buffCpy[3] == 'T')
  {
    return TUI_DT_Parse(buffCpy,len);

  }
  else if(buffCpy[2] == 'A')
  {
    return TUI_AUX_Parse(buffCpy,len);
  }
  else if(buffCpy[2] == 'G' && buffCpy[3] == 'C')
  {
    return TUI_GC_Parse(buffCpy,len);
  }
  

  return 0;
}

int TUI_readStatus_Temperature()
{
  char buff[20] = "DTssMT\n";
  printf(buff);
  packet_append(DRTC_PORT, buff, strlen(buff)+1);

}
int TUI_readStatus_Pressure()
{
  char buff[20] = "DTssPR\n";
  printf(buff);
  packet_append(DRTC_PORT, buff, strlen(buff)+1);

}
int TUI_readStatus_CHStatus(char CHItem)
{
  char buff[20];
  sprintf(buff,"A%dssST\n",CHItem+3);
  printf(buff);
  packet_append(DRTC_PORT, buff, strlen(buff)+1);
}

int TUI_updateRunState()
{
  int i=0;
  switch(m_UserConfig.StartRun)
  {
  case 0://auto
    {
      for(i=0;i<TUI_CH_NUM;i++)
        if(m_TUIStatus.CHs[i].isON != 0)
          break;
      if(i == TUI_CH_NUM)
        break;

      if(( Inst_Status.Pneu.Aux.Aux_3.Ready.Pres.State == IN_CONTROL_ || m_TUIStatus.CHs[TUI_AUX3].isON == 0) &&
        ( Inst_Status.Pneu.Aux.Aux_4.Ready.Pres.State == IN_CONTROL_ || m_TUIStatus.CHs[TUI_AUX4].isON == 0) && 
        ( Inst_Status.Pneu.Aux.Aux_5.Ready.Pres.State == IN_CONTROL_ || m_TUIStatus.CHs[TUI_AUX5].isON == 0) &&
        Run_State == RUN_IDLE)
      {
        mLog("---------------Auto Run Begin-----------------\n");
        TUI_setRun(TUI_RUN_START);
      }
      break;

    }
  case 1://manual
    break;
  case 2://TTL
    break;
  default:
    break;
  }
  return 1;
}

int TUI_readStatus()
{
  int i=0;
  TUI_ReadStep ++ ;
  if(TUI_ReadStep > 10)
    TUI_ReadStep = 0;
  switch(TUI_ReadStep)
  {
  case 1:
    TUI_readStatus_Temperature();
    break;
  case 2:
    TUI_readStatus_Pressure();
    break;
  case 3:
    for(i=0;i<TUI_CH_NUM;i++)
      TUI_readStatus_CHStatus(i);
    break;
  }
  return TUI_updateRunState();
}
